package com.bootdo.index.service.impl;

import com.bootdo.common.solr.BaseSolr;
import com.bootdo.common.solr.PageInfo;
import com.bootdo.common.solr.RowBounds;
import com.bootdo.index.domain.User;
import com.bootdo.index.service.UserIndexService;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/10.
 */
@Service
public class UserIndexServiceImpl implements UserIndexService {



    @Autowired
    private BaseSolr baseSolr;



    private static String defaultCollection = "new_core";

    public boolean saveUser() {

        User user = new User();
        user.setId("2");
        user.setTitle("test111");
        try {
            baseSolr.add(defaultCollection,user);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }

        return true;
    }

    @Override
    public List<User> list() {
        //查询第二种方式
        int page = 1;
        int rows = 10;

        List<User> users = new ArrayList<User>();
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        try {
            PageInfo pageInfo = baseSolr.query(defaultCollection, User.class, query,new RowBounds(0,2));
            users = pageInfo.getList();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }

        return users;
    }
}
